package xyf.p01.array_nodelist;

import commons.ListNode;

/**
 * ClassName: P0160_GetIntersectionNode
 * Description:
 * Author: xyf
 * Date: 9/7/22 7:45 PM
 * Version: 1.0
 **/
public class P0160_GetIntersectionNode
{
    public ListNode getIntersectionNode(ListNode headA, ListNode headB)
    {
        if (headA == null || headB == null)
        {
            return null;
        }

        ListNode curA = headA;
        ListNode curB = headB;
        while (curA != curB)
        {
            if (curA == null)
            {
                curA = headB;
            }
            else
            {
                curA = curA.next;
            }

            if (curB == null)
            {
                curB = headA;
            }
            else
            {
                curB = curB.next;
            }
        }
        return curA;
    }
}
